﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Maps.MapControl;

namespace GeoTracer
{
    public partial class MainPage : UserControl
    {
        private ScaleTransform transform = new ScaleTransform();
        private SolidColorBrush redBrush = new SolidColorBrush();
        private SolidColorBrush greenBrush = new SolidColorBrush();
        private SolidColorBrush violetBrush = new SolidColorBrush();
        private SolidColorBrush orangeBrush = new SolidColorBrush();

        public MainPage()
        {
            InitializeComponent();
            Pushpin p = new Pushpin();
            Server.ServiceClient srv = new Server.ServiceClient();
            srv.GetLocationsCompleted += new EventHandler<Server.GetLocationsCompletedEventArgs>(srv_GetLocationsCompleted);
            srv.GetLocationsAsync();
            greenBrush.Color = Colors.Green;
            orangeBrush.Color = Colors.Orange;
            redBrush.Color = Colors.Red;
            violetBrush.Color = Colors.Purple;
            transform = new ScaleTransform();
            transform.ScaleX = 0.25;
            transform.ScaleY = 0.25;
        }

        private void srv_GetLocationsCompleted(object sender, Server.GetLocationsCompletedEventArgs e)
        {
            if (e.Error != null)
                return;
            foreach (GeoTracer.Server.IPLocation loc in e.Result)
            {
                Pushpin p = new Pushpin();
                string tip = loc.Country;
                if (!string.IsNullOrEmpty(loc.City))
                    tip += ", " + loc.City;
                if (!string.IsNullOrEmpty(loc.Text))
                    tip += " (" + loc.Text + ")";

                ToolTipService.SetToolTip(p, tip);
                p.Location = new Location(loc.Latitude, loc.Longtitude);
                if (loc.Frequency <= 300)
                    p.Background = greenBrush;
                else if (loc.Frequency > 300 && loc.Frequency <= 500)
                    p.Background = orangeBrush;
                else if (loc.Frequency > 500 && loc.Frequency < 2000)
                    p.Background = redBrush;
                else if (loc.Frequency >= 2000)
                    p.Background = violetBrush;
                p.RenderTransform = transform;
                map.Children.Add(p);
            }
        }
    }
}
